package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.DeleteById;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {

    @Override
    public ResponseResult findByPageAndName(SensitiveDto sensitiveDto) {
        //判断参数
        if(sensitiveDto == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        sensitiveDto.checkParam();
        //添加查询条件
        //1添加分页条件
        Page<AdSensitive> page = new Page<>(sensitiveDto.getPage(),sensitiveDto.getSize());
        //2添加查询条件
        LambdaQueryWrapper<AdSensitive> queryWrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(sensitiveDto.getName())){
            queryWrapper.like(AdSensitive::getSensitives,sensitiveDto.getName());
        }
        //查询
        IPage<AdSensitive> result = page(page,queryWrapper );

        //封装查询结果
        PageResponseResult pageResponseResult = new PageResponseResult(sensitiveDto.getPage(),sensitiveDto.getSize(),(int)result.getTotal());
        pageResponseResult.setData(result.getRecords());
        return pageResponseResult;
    }

    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        //检查参数
        if(adSensitive == null){
            ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //添加
        save(adSensitive);

        //返回信息
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult updates(AdSensitive adSensitive) {
        //检查参数
        if(adSensitive == null || adSensitive.getId() == null){
            ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //修改
        updateById(adSensitive);

        //返回信息
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult delete(Integer id) {
        //检查参数
        if(id == null){
            ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }

        //删除
        removeById(id);

        //返回信息
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
